<?php
defined('BASEPATH') OR exit('No direct script access allowed');
// +----------------------------------------------------------------------
// | 后台用户管理中心，添加会员（管理员与会员为同一张表）
// +----------------------------------------------------------------------
// | Copyright (c) 2015
// +----------------------------------------------------------------------
// | Author: baiping 125618036@qq.com http://www.webipcode.com
// +----------------------------------------------------------------------
// | 2015-7-10下午5:05:01
// +----------------------------------------------------------------------

class Member extends MY_Controller{
	
	
	public function __construct(){
		parent::__construct();
	}
	
	/**
	 * 用户管理首页
	 * @author Baip
	 */
	public function index(){
		$where = array();
		//搜索
		$search_name = $this->input->get_post('search_name');
		if ( !empty( $search_name ) && !is_numeric( $search_name )  ){
			$where = "username LIKE '%$search_name%' OR email LIKE '%$search_name%' ";
			$suffix = urldecode( '?search_name='.$search_name );//如果url中除了页码参数还有其它参数时
		}elseif ( is_numeric( $search_name ) ){
			$where = " uid = " .$search_name ." OR username = ".$search_name;  
			$suffix = urldecode( '?search_name='.$search_name );//如果url中除了页码参数还有其它参数时
		}
		
		//载入网站配置模型
		$this->load->model('Member_model','member');
		$total = $this->member->getCount($where);//获取数据记录数
		
		//分页
		$this->load->library('pagination');//载入分页类
		
		$page_size = 10;//每页显示的记录数
		$url_segment = $this->uri->segment(3);//获取url中的第三段为当前页码
		//获取分页配置
		$suffix = empty( $suffix ) ? '' : $suffix;
		$config = get_page_config(site_url('member/index'),$total, $page_size, 3, $suffix, site_url('/member/index/1/').$suffix);
		$this->pagination->initialize($config);
		$field="uid,username,email,last_login_time,login_num,last_login_ip,status,reg_time";
		$offset = intval( $url_segment );//获取当前页码
		//获取偏移数，如果当前页码大于0，用当前页码－1*每页记录数
		$offset = $offset > 0 ? ( $offset - 1  ) * $page_size : $offset;
		$list = $this->member->getData($where, $field, $page_size, $offset);//获取数据
		$pages = $this->pagination->create_links();//显示分页
		//渲染数据
		$this->load->vars('meta_title', '用户管理');
		$this->load->vars('list', $list);
		$this->load->vars('record_count', $total);
		$this->load->view('member/index');
	}
	
	/**
	 * 添加用户
	 */
	public function create(){
		//引入表单验证类
		$this->load->library('form_validation');
		//验证表单及是post提交的时候才可以入库
		if( $this->form_validation->run('member') && IS_POST){
			$this->load->model('Member_model','member');//载入config模型
			$insert_id = $this->member->addData();
			if ( $insert_id ){
				$this->show_message('success','添加成功');
			}else{
				$this->show_message('error','添加失败');
			}
		}else{
			$this->load->vars('meta_title', '添加用户');
			$this->load->view('member/create');
		}	
	}
	
	/**
	 * 改变用户状态或删除等操作
	 * @param string $method 操作方式
	 * @param int $uid 用户UID
	 */
	public function changesstatus(){
		
		//参数接收
		$method = $this->uri->segment(3);//操作方式
		//判断是post方式还是get方式
		if ( IS_POST ){
			$uid = $this->input->post('uid', TRUE);//post接收删除的uid值
		}else{
			$uid = intval( $this->uri->segment(4) );//get 接收的uid值
		}
		if ( empty( $method ) && $uid <= 0 ){
			$this->show_message('error', '参数错误', site_url('member/index') );
		}
		$action_val= $this->input->get_post('status_val');//状态值
		
		$this->load->model('Member_model', 'member');
		switch ($method){
			//改变状态
			case 'disable':
			$query_status = $this->setMemberStatus($uid, $action_val);
			break;
			//删除用户
			case 'delete':
				if ( 1 == $uid || in_array('1', $uid) ){
					ajaxreturn(0,'此用户不能删除', site_url('member/index'));
				}
				$query_status = $this->member->deleteData($uid,2, 'uid');
		}
		
		if( $query_status ){
			ajaxreturn(1,'操作成功', site_url('member/index'));
		}else{
			ajaxreturn(0,'操作失败', site_url('member/index'));
		}
	}
	
	/**
	 * 对用户状态进行操作
	 * @param unknown_type $uid
	 * @param unknown_type $action_val
	 */
	private function setMemberStatus($uid,$action_val){
		$type = is_array( $uid ) ? 2 : 1;
		$query_status = $this->member->updateData($uid,$type,'uid', array('status'=>$action_val));
		return $query_status;
	}
	
	/**
	 * 修改密码
	 * @param unknown_type $uid
	 */
	public function editpass(){
		//参数接入验证
		$uid = IS_POST ? intval($this->input->post('uid')) : intval($this->uri->segment(3));
		if ( $uid == 0 ){
			$this->show_message('error', '参数错误', site_url('member/index') );
		}
		
		$this->load->model('Member_model', 'member');//引入用户模型
		//引入表单验证类
		$this->load->library('form_validation');
		if ( IS_POST ){
			//验证表单密码
			$this->form_validation->set_rules('password', '密码', 'trim|required|min_length[5]|max_length[12]|alpha_numeric');
			if ($this->form_validation->run() == true){
				$status = $this->member->updatePwd($uid);
				if ( $status ){
					$this->show_message('success', '操作成功', site_url('member/index'));
				}else{
					$this->show_message('error', '操作失败', site_url('member/editpass/'.$uid));
				}
			}
			
		}
		//获取用户的uid,username
		$user_info = $this->member->getFind( array('uid'=>$uid), 'uid,username' );
		$this->load->vars('meta_title', '修改密码');
		$this->load->vars('user_info', $user_info);
		$this->load->view('member/editpass');
	}
	
	
}